package com.sgb.kjwl.viewmodel.machine;

import android.util.Log;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishLeaseInfoModel2 extends ViewModel {

    public MutableLiveData<Integer> selectFragLiveData = new MutableLiveData<>();
    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> showLoad = new MutableLiveData<>();
    public MutableLiveData<Boolean> showLoad2 = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> initAddressDataLiveData = new MutableLiveData<>();

    public MutableLiveData<List<UnitEntity>> showUnitLiveData = new MutableLiveData<>();
    public MutableLiveData<String> isSuccessDataLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>> showTypeViewLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> getAllPayWayLiveData = new MutableLiveData<>();
    public MutableLiveData<MapLongAndLatEntity> getLocationLLLiveData = new MutableLiveData<>();//废弃
    public MutableLiveData<List<DictEntity3>> getModelSelectListLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> showVehicleSystemPickerLiveData = new MutableLiveData<>();

    public MutableLiveData<Integer> submitLiveData = new MutableLiveData<>();
    public PublishLeaseDataEntity bean = new PublishLeaseDataEntity();
    public MutableLiveData<PublishLeaseDataEntity> showDetailLiveData = new MutableLiveData<>();//详情数据

    //  发布求租信息
    public void getequimentLeasing() {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getequimentLeasing2(bean).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    msgLiveData.postValue("发布失败..");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String no = response.body().getData();
                    isSuccessDataLiveData.postValue(no);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                showLoad.postValue(false);
                msgLiveData.postValue("发布失败..");
            }
        });
    }

    public void getDeviceDetail(String id, String type) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getDeviceDetail(id, type).enqueue(new Callback<BaseEntity<PublishLeaseDataEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PublishLeaseDataEntity>> call, Response<BaseEntity<PublishLeaseDataEntity>> response) {
                showLoad2.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    showDetailLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PublishLeaseDataEntity>> call, Throwable t) {
                showLoad.postValue(false);
            }
        });
    }

    //获取省市区
    public void getAddressModel() {
        showLoad.postValue(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    initAddressDataLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                showLoad.postValue(false);
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //  车辆租赁单位选择
//    public void getNeedLeaseCarPrice() {
//        showLoad.postValue(true);
//        APIManager.getInstance().getMachineAPI().getNeedLeaseCarPrice().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
//                showLoad.postValue(false);
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK))
//                    showUnitLiveData.postValue(response.body().getData());
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
//                showLoad.postValue(false);
//            }
//        });
//    }

    //房子出租单位选择
//    public void getEstatePriceUnit() {
//        showLoad.postValue(true);
//        APIManager.getInstance().getMachineAPI().getEstatePriceUnitNew().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
//                showLoad.postValue(false);
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK))
//                    showUnitLiveData.postValue(response.body().getData());
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
//                showLoad.postValue(false);
//            }
//        });
//    }

    //  获取机械价格单位接口
//    public void getUnit() {
//        showLoad.postValue(true);
//        APIManager.getInstance().getMachineAPI().getUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
//                showLoad.postValue(false);
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK))
//                    showUnitLiveData.postValue(response.body().getData());
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
//                showLoad.postValue(false);
//            }
//        });
//    }

// 获取设备价格单位接口
//    public void getRentpriceUnit() {
//        showLoad.postValue(true);
//        APIManager.getInstance().getMachineAPI().getRentpriceUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
//                showLoad.postValue(false);
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    showUnitLiveData.postValue(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
//                showLoad.postValue(false);
//            }
//        });
//    }


    //获取类型分类数据
    public void getTypeModel() {
        APIManager.getInstance().getMachineAPI().getAllTypeList().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        showTypeViewLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Throwable t) {
                Log.v("dd", "sdsdd");
            }
        });
    }


    // 获取付款方式
    public void getAllPayWay() {
        APIManager.getInstance().getMachineAPI().getAllPayWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        getAllPayWayLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    //获取经纬度信息
    public void getLocationLL(String provinceNo, String cityNo, String districtNo) {
        showLoad.postValue(true);
        APIManager.getInstance().getDriverAPI().getLocationLL("610000", provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getLocationLLLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                showLoad.postValue(false);
                msgLiveData.postValue(t.getMessage());
            }
        });

    }

    //获取汽车类型分类信息
    public void getModelSelect() {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getModelSelect().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getModelSelectListLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                showLoad.postValue(false);
            }
        });
    }

    /**
     * 获取车系列表
     */
    public void getCarSeries(String mBrandNo) {
        APIManager.getInstance().getMachineAPI().getCarSeries(mBrandNo).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body() != null && response.body().getState().equals("ok")) {
                    showVehicleSystemPickerLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }
}
